//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall iSwap(int *a,int *b)
{
    int tmp;
    tmp=*a;
    *a=*b;
    *b=tmp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;h<bmp->Height;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=pt1+bmp->Width-1;
        while(pt1<pt2)
        {
            iSwap(pt1,pt2);
            pt1++;
            pt2--;
        }
    }
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;h<bmp->Height/2;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=(int *)bmp->ScanLine[bmp->Height-h-1];
        for(int w=0;w<bmp->Width;w++)
        {
            iSwap(pt1,pt2);
            pt1++;
            pt2++;
        }
    }
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;h<bmp->Height/2;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=(int *)bmp->ScanLine[bmp->Height-h-1];
        pt2=pt2+bmp->Width-1;
        for(int w=0;w<bmp->Width;w++)
        {
            iSwap(pt1,pt2);
            pt1++;
            pt2--;
        }
    }
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    unsigned char *cpt;
    unsigned char  tmp;

    for(int h=0;h<bmp->Height;h++)
    {
        cpt=(unsigned char *)bmp->ScanLine[h];
        for(int w=0;w<bmp->Width;w++)
        {
            //tmp=(cpt[0]+cpt[1]+cpt[2])/3;      //방법1
            tmp=(cpt[0]+cpt[1]*4+cpt[2]*3)>>3;   //방법2    ==> 이방법이 쬐금더 빠름
            cpt[0]=tmp;
            cpt[1]=tmp;
            cpt[2]=tmp;
            cpt+=4;
        }
    }
    
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}
//---------------------------------------------------------------------------
